//
//  HomeHeaderMessageView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/21.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "HomeHeaderMessageView.h"

#import "BCTimerHandler.h"

@interface HomeHeaderMessageContentView ()

@property (nonatomic, strong) QMUILabel *messLabel;

@property (nonatomic, strong) QMUILabel *animateMessLabel;

@end

@implementation HomeHeaderMessageContentView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.clipsToBounds = true;
        //
        self.messLabel = [QMUILabel new];
        self.messLabel.font = [self.class messageFont];
        self.messLabel.textColor = [UIColor whiteColor];
        [self addSubview:self.messLabel];
        [self.messLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
        //
        self.animateMessLabel = [QMUILabel new];
        self.animateMessLabel.font = self.messLabel.font;
        self.animateMessLabel.textColor = [UIColor whiteColor];
        [self addSubview:self.animateMessLabel];
        [self.animateMessLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
        }];
        //
        self.animateMessLabel.numberOfLines = self.messLabel.numberOfLines = 2;
        //
        [self bringSubviewToFront:self.messLabel];
    }
    return self;
}


- (void)setMessage:(BCHomeDataMessage *)message needAnimate:(BOOL)animate{
    BCHomeDataMessage *aboveMess = _message;
    _message = message;
    //
    self.messLabel.text = message.mess;
    self.animateMessLabel.text = aboveMess.mess;
    //
    CGFloat offset = [self.class defaultHeight];
    if (animate) {
        self.messLabel.transform = CGAffineTransformMakeTranslation(0, offset);
        self.animateMessLabel.transform = CGAffineTransformMakeTranslation(0, 0);
        [UIView animateWithDuration:.75 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.messLabel.transform = CGAffineTransformMakeTranslation(0, 0);
            self.animateMessLabel.transform = CGAffineTransformMakeTranslation(0, -offset);
        } completion:^(BOOL finished) {
            self.messLabel.transform = CGAffineTransformMakeTranslation(0, 0);
            self.animateMessLabel.transform = CGAffineTransformMakeTranslation(0, -offset);
        }];
    }else {
        self.messLabel.transform = CGAffineTransformMakeTranslation(0, 0);
        self.animateMessLabel.transform = CGAffineTransformMakeTranslation(0, -offset);
    }
}

+ (UIFont *)messageFont {
    return [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
}
+ (CGFloat)defaultHeight {
    return [self messageFont].lineHeight*2+2;
}

@end


@interface HomeHeaderMessageView () <BCTimerHandlerDelegate>

@property (nonatomic, strong) UIImageView *messageIV;

/**
 *BCTimerHandler
 */
@property (nonatomic, strong, nullable) BCTimerHandler *scrollTimer;

/**
 *currentMessageIndex
 */
@property (nonatomic, assign) NSInteger currentMessageIndex;

@end

@implementation HomeHeaderMessageView

- (BCTimerHandler *)scrollTimer {
    if (!_scrollTimer) {
        _scrollTimer = [[BCTimerHandler alloc] init];
        _scrollTimer.timeInterval = 5;
        _scrollTimer.delegate = self;
    }
    return _scrollTimer;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.messageContent = [HomeHeaderMessageContentView new];
    [self addSubview:self.messageContent];
    CGFloat defaultHeight = [HomeHeaderMessageContentView defaultHeight];
    [self.messageContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(25.0);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(defaultHeight);
        make.bottom.mas_equalTo(self);
    }];
    //
    self.messageIV = [UIImageView new];
    self.messageIV.image = LOAD_IMAGE(@"home_header_voice");
    [self addSubview:self.messageIV];
    [self.messageIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.centerY.mas_equalTo(self.messageContent);
        make.size.mas_equalTo(CGSizeMake(20.0, 20.0));
    }];
    self.messageIV.alpha = 0;
}


- (void)setMessages:(NSArray *)messages {
    _messages = messages;
    if (_messages.count) {
        self.messageIV.alpha = 1;
        //
        if (!self.messageContent.message) {
            self.currentMessageIndex = 0;
            [self.messageContent setMessage:messages[self.currentMessageIndex] needAnimate:false];
            if (messages.count>1) {
                [self.scrollTimer start];
            }else {
                [self endScrllTimer];
            }
        }
    }else {
        self.messageIV.alpha = 0;
        [self endScrllTimer];
    }
}

- (void)endScrllTimer {
    if (_scrollTimer) {
        [_scrollTimer stop];
        _scrollTimer.delegate = nil;
        _scrollTimer = nil;
    }
}

#pragma mark - BCTimerHandlerDelegate
- (void)timerHandlerRepeatsAction {
    NSInteger messagesCount = self.messages.count;
    if (messagesCount==0) {
        [self endScrllTimer];
        return;
    }
    //
    self.currentMessageIndex += 1;
    //
    if (self.currentMessageIndex>=messagesCount) {
        self.currentMessageIndex = 0;
    }
    [self.messageContent setMessage:self.messages[self.currentMessageIndex] needAnimate:true];
    if (messagesCount<=1) {
        [self endScrllTimer];
    }
}


- (void)dealloc {
    [self endScrllTimer];
}

+ (CGFloat)defaultHeight {
    return [HomeHeaderMessageContentView defaultHeight];
}

@end
